﻿using Lan.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lan.Business
{
    public class XT_TableRepository : BaseRepository<XT_Table>, IXT_TableRepository
    {
        public void Add(XT_Table model)
        {
            if (model.Id == Guid.Empty)
            {
                model.Id = Guid.NewGuid();
            }
            Db.Deleteable<XT_Table>(model.Id).ExecuteCommand();
            Db.Deleteable<XT_Column>(c => c.TableId == model.Id).ExecuteCommand();
            //model.Id = Guid.NewGuid();
            foreach (XT_Column item in model.Columns)
            {
                item.Id = Guid.NewGuid();
                item.TableId = model.Id;
            }
            Db.Insertable(model).ExecuteCommand();
            Db.Insertable(model.Columns).ExecuteCommand();
        }

        public void CreateTable(string sql)
        {
            Db.Ado.ExecuteCommand(sql);
        }

        public XT_Table Find(Guid id)
        {
            XT_Table entity = Db.Queryable<XT_Table>()
                .Mapper(t => t.Columns, t => t.Columns.First().TableId)
                .Where(t => t.Id == id).First();
            return entity;
        }

        public void AddColumn()
        {
            //Db.DbMaintenance.UpdateColumn("",);
        }
    }
}
